package com.proj.hot.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.proj.hot.GlobalParams;
import com.proj.hot.R;
import com.proj.hot.utils.DebugUtil;

import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * 用户信息修改界面
 */
public class UserInfoUpdateActivity extends Activity {

    private EditText userInfoNameEd;//名字
    private EditText userInfoPhoneEd;//电话
    private EditText userInfoAddressEd;//地址

    private Button userInfoUpdate; //修改信息按钮
    private Button userInfoCancel; //返回上一页

    private String accountid = "";
    private String name = "";
    private String phone = "";
    private String address = "";
    private JSONObject jsonObject;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_userinfo_update);
        Intent i = getIntent();
        name = i.getStringExtra("name");
        phone = i.getStringExtra("phone");
        address = i.getStringExtra("address");
        initView();
        setOnListener();
    }

    private void initView() {
        userInfoNameEd = (EditText) this.findViewById(R.id.user_update_name);
        userInfoPhoneEd = (EditText) this.findViewById(R.id.user_update_phone);
        userInfoAddressEd = (EditText) this.findViewById(R.id.user_update_address);
        userInfoUpdate = (Button) this.findViewById(R.id.user_update_sure);
        userInfoCancel = (Button) this.findViewById(R.id.user_update_cancel);

        userInfoNameEd.setSingleLine();
        userInfoPhoneEd.setSingleLine();
        userInfoAddressEd.setSingleLine();

        userInfoNameEd.setText(name);
        userInfoPhoneEd.setText(phone);
        userInfoAddressEd.setText(address);
    }

    private void setOnListener() {
        userInfoUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                accountid = GlobalParams.user_id;
                name = userInfoNameEd.getText().toString().trim();
                phone = userInfoPhoneEd.getText().toString().trim();
                address = userInfoAddressEd.getText().toString().trim();
                DebugUtil.i("UserInfoUpdateActivity", "修改的=accountid" + accountid + ",name=" + name + ",phone" + phone + ",address=" + address);
                String phoneOneNum = "";
                int phoneLength = phone.length();
                if (phone.equals("")) {
                    DebugUtil.toast(UserInfoUpdateActivity.this, "修改的手机号码不能为空！");
                    return;
                } else {
                    phoneOneNum = phone.substring(0, 1);
                    DebugUtil.i("pppp", "phone=" + phoneOneNum);
                }
                if (!phoneOneNum.equals("1")) {
                    DebugUtil.toast(UserInfoUpdateActivity.this, "请输入正确的手机号码！");
                    return;
                }
                if (phoneLength != 11) {
                    DebugUtil.toast(UserInfoUpdateActivity.this, "请输入11位手机号码！");
                    return;
                }
                updateData(accountid, name, phone, address);
            }
        });
        userInfoCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UserInfoUpdateActivity.this.finish();
            }
        });
    }

    private void updateData(String accountid, String name, String phone, String addr) {

        try {


            /**
             * ---------------body------------------
             */
            JSONObject body = new JSONObject();
            body.put("accountid", accountid);
            body.put("name", name);
            body.put("phone", phone);
            body.put("address", addr);

            RequestParams params = new RequestParams(GlobalParams.reqHost + "account/update");
            params.setAsJsonContent(true);
            params.setBodyContent(body.toString().trim());

            /**
             * ---------------send------------------
             */
            x.http().post(params, new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String s) {
                    try {
                        DebugUtil.i("UserInfoUpdateActivity", "用户信息返回数据=" + s);
                        JSONObject jData = new JSONObject(s);
                        jsonObject = jData.getJSONObject("resphead");

                        String resultcode = jsonObject.getString("resultcode");
                        if (resultcode.equals("0000")) {
                            DebugUtil.toast(UserInfoUpdateActivity.this, "修改成功!");
                            DebugUtil.i("UserInfoUpdateActivity", "用户信息数据修改成功");
                            UserInfoUpdateActivity.this.finish();
                        } else {
                            DebugUtil.toast(UserInfoUpdateActivity.this, "修改失败!");
                            DebugUtil.i("UserInfoUpdateActivity", "用户信息数据修改失败resultcode=" + resultcode);
                            UserInfoUpdateActivity.this.finish();
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }

                @Override
                public void onFinished() {

                }

                @Override
                public void onCancelled(CancelledException e) {

                }

                @Override
                public void onError(Throwable throwable, boolean b) {
                    DebugUtil.toast(UserInfoUpdateActivity.this, "修改失败,未知错误!");
                    DebugUtil.i("UserInfoUpdateActivity", "用户信息数据修改失败，未知错误：xutil.onError()");
                    UserInfoUpdateActivity.this.finish();
                }
            });


        } catch (Exception e) {
            e.printStackTrace();
            DebugUtil.i("UserInfoUpdateActivity", "修改用户信息异常！");
            DebugUtil.i("UserInfoUpdateActivity", "e.printStackTrace()=" + e.toString());
        }


    }

}
